package main

import (
	"fmt"
	"request"
	"data"
	"parse"
	
	
)

func main() {
	
	var requesturl string="http://s.cjol.com/l2008-kw-%E9%AB%98%E9%80%9A/?SearchType=3"
	//var requesturl string="http://s.cjol.com/l2008-kw-java/?SearchType=3"
	requestbodyfirst :=request.Request(requesturl);
	
	p,err :=parse.Parse(requestbodyfirst)
	if(err!=nil){
		panic(err)
	}
	pageNodes :=p.Find("#pagination_list").Html()
	
	pagelists,_:=parse.Parse(pageNodes)
	pagelistHrefs :=pagelists.Find("a").Attrs("href")
	pagelen:=len(pagelistHrefs)
	var requestUrls []string = make([]string, pagelen)
	for i,t:=range pagelistHrefs{
		requestUrls[i]=t
	}
	requestUrls[pagelen-1]=requesturl
	
	for _,m:=range requestUrls{
		requestbody :=request.Request(m);
	    p1,_ :=parse.Parse(requestbody)
		bodyNodes :=p1.Find("#resultlist_div").Html()
		joblist,_:=parse.Parse(bodyNodes)
		for _, v :=range joblist.Find(".results_list_box").HtmlAll(){
				companynameNodes,_:=parse.Parse(v)
				companynameContent :=companynameNodes.Find(".list_type_second").Html();
				companynameContentNodes,_:=parse.Parse(companynameContent);
				//公司名称
				companyname:=companynameContentNodes.Find("a").Text()
	
				//公司信息路径
				companyhref:=companynameContentNodes.Find("a").Attrs("href")
				
				informationBody :=request.Request(companyhref[0])
				informationNodes,_:=parse.Parse(informationBody)
				companyInformationNodes :=informationNodes.Find("dl").Html()
				industryNodes,_:=parse.Parse(companyInformationNodes)
				industryname:=industryNodes.Find("dd").Text()
				
				fmt.Println(companyname)
				fmt.Println(companyhref[0])
				fmt.Println(industryname)
				fmt.Println("-----")
				data.Insert(companyhref[0],companyname,industryname)
				
		}
	}
	
}
